Numpy and Matplotlib

Paul M. Magwene

2024-02-04

Numpy

Numpy is the de facto library for numerical computing in Python.

  • Provides an efficient multi-dimensional array data structure that facilitates a wide variety of numerical computing tasks.

  • Provides a wide range of functions for array manipulation and core numerical functions that are important for many numerical tasks in mathematics, statistics and machine learning, bioinformatics, etc.

  • Basis for many other packages

Matplotlib

Matplot is the de facto library for plotting in Python.

  • Provides to “flavors” of plotting interfaces
    1. A function based interface through matplotlib.pyplot
    2. An object oriented interface
  • The function based interface tends to be “higher level” while the object oriented interface provides greater flexibility and tweaking. Not uncommon to mix the two types of interfaces when constructing plots

Imports for code examples

import numpy as np
from matplotlib import pyplot as plt  

Numpy: key concepts

  • Numpy arrays (type: ndarray) are multi-dimensional, ordered, and homogenous

  • Arrays have a data type (dtype) which specifies the types of the data they hold. You can leave this up to numpy to infer/guess, or you can specify dtype explicitly

  • Arrays have a size, dimension, and shape

  • Array access is a logical extension of indexing for other Python data structures like lists but there are some subtleties to be aware of

  • Many Numpy operators and functions are vectorized and are applied to every element in an input array

Numpy array creation and dimensional attributes

a = np.array([1,2,3,4,5,6])  
a
array([1, 2, 3, 4, 5, 6])
a.dtype, a.size, a.shape, a.ndim
(dtype('int64'), 6, (6,), 1)
a[0], a[-1]
(1, 6)
a[2:4], a[::2]
(array([3, 4]), array([1, 3, 5]))
b = np.array([[1,2],[3,4]], dtype=float)
b
array([[1., 2.],
       [3., 4.]])
b.dtype, b.size, b.shape, b.ndim
(dtype('float64'), 4, (2, 2), 2)

Note the subtle difference here between the arrays assigned to a and c:

c = np.array([[1,2,3,4,5,6]])
c
array([[1, 2, 3, 4, 5, 6]])
c.dtype, c.size, c.shape, c.ndim
(dtype('int64'), 6, (1, 6), 2)

Array’s don’t have to contain numbers:

s = np.array(["how", "now", "brown", "cow"])
s
array(['how', 'now', 'brown', 'cow'], dtype='<U5')
s.dtype
dtype('<U5')
d3 = np.array([
  [[1,2,3,4]],
  [[5,6,7,8]],
  [[9,10,11,12]]
])
d3
array([[[ 1,  2,  3,  4]],

       [[ 5,  6,  7,  8]],

       [[ 9, 10, 11, 12]]])
d3.shape, d3.ndim
((3, 1, 4), 3)

Image data for examples

We will use 3D Cell data from skimage.data

import skimage
cells3d = skimage.data.cells3d()

Factors

★ Factors: Your turn

What behaviors do you observe in the examples below?

  • Example A

  • Example B

  • Example C